#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include "cppjieba/Jieba.hpp"
#include <jsoncpp/json/json.h>
#include "Log.hpp"

namespace ns_util
{
    using namespace LogModule;

    class FileUtil
    {
    public:
        static bool ReadFile(const std::string &file_path, std::string *out)
        {
            std::ifstream in(file_path, std::ios::in);
            if (!in.is_open())
            {
                std::cerr << "open file " << file_path << " error" << std::endl;
                return false;
            }
            std::string line;
            while (std::getline(in, line))
            { // getline 的返回值是一个 &， while判断的是一个bool， 本质是因为重载了强制类型转换
                *out += line;
            }
            in.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void Split(const std::string &target, std::vector<std::string> *out, const std::string &sep)
        {
            // boost split
            boost::split(*out, target, boost::is_any_of(sep), boost::token_compress_on);
        }
    };

    // 修改 去掉暂停词 
    const char *const DICT_PATH = "./dict/jieba.dict.utf8";
    const char *const HMM_PATH = "./dict/hmm_model.utf8";
    const char *const USER_DICT_PATH = "./dict/user.dict.utf8";
    const char *const IDF_PATH = "./dict/idf.utf8";
    const char *const STOP_WORD_PATH = "./dict/stop_words.utf8";
    class JiebaUtil
    {
    private:
        JiebaUtil() : _jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH)
        {}

        JiebaUtil(const JiebaUtil&) = delete;
        JiebaUtil& operator=(const JiebaUtil&) = delete;
    public:
        static JiebaUtil* get_instance()
        {
            static std::mutex mtx;
            if(nullptr == instance) {
                mtx.lock();
                if(nullptr == instance) {
                    instance = new JiebaUtil();
                    instance->InitJiebaUtil();
                }
                mtx.unlock();
            }

            return instance;
        }

        // 导入暂停词
        void InitJiebaUtil()
        {
            std::ifstream in(STOP_WORD_PATH);
            if(!in.is_open()) {
                LOG(LogLevel::ERROR) << "load stop words file error";
                return ;
            }

            std::string line;
            while(std::getline(in, line)) {
                _stop_words.insert({line, true});
            }

            in.close();
        }

        // 去掉暂停词
        void CutStringHelper(const std::string &src, std::vector<std::string> *out)
        {
            _jieba.CutForSearch(src, *out);
            for(auto iter = out->begin(); iter != out->end(); ) {
                auto it = _stop_words.find(*iter);
                if(it != _stop_words.end()) {
                    // 说明当前的string是暂定词，需要去掉
                    iter = out->erase(iter);
                } else {
                    iter++;
                }
            }
        }

        // 只需要修改这部分 上层无需修改
        static void CutString(const std::string &src, std::vector<std::string> *out)
        {
            ns_util::JiebaUtil::get_instance()->CutStringHelper(src, out);
            // _jieba.CutForSearch(src, *out);
        }

    private:
        // static cppjieba::Jieba jieba;
        cppjieba::Jieba _jieba;
        std::unordered_map<std::string, bool> _stop_words;

        static JiebaUtil *instance;
    };

    JiebaUtil *JiebaUtil::instance = nullptr;

    // cppjieba::Jieba JiebaUtil::jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);

    class JsonUtil
    {
    public:
        // 实现字符串的序列化
        static bool Serialize(const Json::Value &val, std::string &body)
        {
            std::stringstream ss;

            // 先实例化一个工厂类对象
            static Json::StreamWriterBuilder swb;
            // 再使用工厂类对象来生产派生类
            std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
            int ret = sw->write(val, &ss);
            if (ret != 0)
            {
                std::cerr << "Json Serialize failed!" << std::endl;
                return false;
            }

            body = ss.str();
            return true;
        }

        // 实现json字符串的反序列化
        static bool UnSerialize(const std::string &body, Json::Value &val)
        {
            // 实例化工厂类对象
            static Json::CharReaderBuilder crb;
            std::unique_ptr<Json::CharReader> cr(crb.newCharReader());

            std::string errs;
            bool ret = cr->parse(body.c_str(), body.c_str() + body.size(), &val, &errs);
            if (ret == false)
            {
                std::cerr << "Json UnSerialize failed! " << errs << std::endl;
                return false;
            }

            return true;
        }
    };
}